<?php

namespace app\merchant\controller\order;

use app\merchant\controller\Base;
use app\merchant\model\Order as OrderModel;
use app\merchant\model\Express as ExpressModel;

/**
 * 订单操作控制器
 * Class Operate
 * @package app\store\controller\order
 */
class Operate extends Base
{
    /* @var OrderModel $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new OrderModel;
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export()
    {
        $param = $this->request->param();

        if (!$this->model->exportList($param)) {
            return $this->renderError($this->model->getError() ?: '操作失败');
        }
        return $this->model->exportList($this->request->param());
    }

    /**
     * 批量发货获取物流公司
     * @return array|bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function expressList()
    {

        $express_list = ExpressModel::getAll();
        return $this->renderSuccess($express_list,'查询成功');

    }

    /**
     * 批量发货
     * @return array|bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function batchDelivery()
    {

        if ($this->model->batchDelivery($this->postData('order'))) {
            return $this->renderSuccess([],'发货成功');
        }
        return $this->renderError([],$this->model->getError() ?: '发货失败');
    }

    /**
     * 批量发货模板
     */
    public function deliveryTpl()
    {
        return $this->model->deliveryTpl();
    }




    /**
     * 审核：用户取消订单
     * @param $order_id
     * @return array|bool
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function confirmCancel()
    {
        $param = $this->postData();
        if(!isset($param['order_id']) || isInt($param['order_id'])){
            return $this->renderError([],'参数错误');
        }

        $model = OrderModel::detail($param['order_id']);

        if ($model->confirmCancel($param)) {
            return $this->renderSuccess([],'审核成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');
    }

    /**
     * 门店自提核销
     * @param $order_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function extract()
    {
        $param = $this->postData();
        if(!isset($param['order_id']) || isInt($param['order_id'])){
            return $this->renderError([],'参数错误');
        }
        if(!isset($param['extract_clerk_id']) || isInt($param['extract_clerk_id'])){
            return $this->renderError([],'请选择核销员');
        }

        $model = OrderModel::detail($param['order_id']);

        if ($model->verificationOrder($param['extract_clerk_id'])) {
            return $this->renderSuccess([],'核销成功');
        }
        return $this->renderError([],$model->getError() ?: '核销失败');
    }

}
